<?php
session_name(DicomScience);
session_start();

/*
 * Controller for Update and Delete Media listens on http_post option author and message. The Data is stored
 * into the Database. The return object will be nothing. 
 */
// Getting global settings
require_once('../includes/gset.php');

// Defining response language
$session_language = $_SESSION['lang'];
if ($session_language == "") {
	
	// Assuming Controller has not been triggered by a PHP Session
	if ($lang == 1)	{

		require_once ('../includes/language/german.php');	
			
	} elseif($lang == 2){

		require_once ('../includes/language/english.php');
	
	} elseif($lang == 3){

		require_once ('../includes/language/french.php');
	
	}	
	
}

$blogdate = time();

$medAction = $_POST['action'];
$medId = $_POST['mediaId'];
$editor = $_POST['editor'];
$userName = $_POST['userName'];
$tags = $_POST['tags'];
$modality = $_POST['modality'];
$organ = $_POST['organ'];
$mediaType = $_POST['mediaType'];

include ('../includes/database/connect.php');
$sql_checkUserid = "SELECT id_staff, username, firstname, lastname from staff WHERE id_staff = '$editor'";

$checkUserid = mysql_query($sql_checkUserid);		
	
	while($id = mysql_fetch_object($checkUserid)){
	
		$usrid = $id->id_staff;
	
	}
if ($usrid == $editor){
	
	/*
	 * ACTION CONTROLLER: WHATTA DO - DEPENDING ON THE ACTION FLAG?
	 */
	if ($medAction == '1'){
		
		$sql_getMediaRep = "SELECT medrep, imgrep FROM media WHERE id_media = '$medId'";
		$sql_do = mysql_query($sql_getMediaRep);
		
		while ($rep = mysql_fetch_object($sql_do)){
			
			$medRep = $rep->medrep;
			$medThb = $rep->imgrep;
						
		}
		
		$sql_deleteMedia = "DELETE FROM media WHERE id_media = '$medId'";
		$sql_do = mysql_query($sql_deleteMedia);
		
		# UnLinking Media and Thumbnail...
		unlink($medRep);
		unlink($medThb);
		
		echo "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>";
		echo "<newMedia>";
		echo "<status>success</status>";
		echo "</newMedia>";
		
	
	} elseif ($medAction == '2'){
	
		$sql_updateMedia = "UPDATE media SET tags = '$tags', modality = '$modality', organ = '$organ', media_type = '$mediaType' WHERE id_media = '$medId'";
		$sql_do = mysql_query($sql_updateMedia);
	
		echo "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>";
		echo "<newMedia>";
		echo "<status>success</status>";
		echo "</newMedia>";
	
	} 
	
} else {
	
		echo "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>";
		echo "<newMedia>";
		echo "<status>error</status>";
		echo "</newMedia>";
	
}
include ('../includes/database/closedb.php');
?>